import Vue from 'vue'
import VueRouter from 'vue-router'
import LoginView from "@/views/LoginView.vue";
import store from "@/store";
import Message from "element-ui/packages/message/src/main";
Vue.use(VueRouter) //给Vue对象中加入 VueRouter组件

// 定义路由规则
const routes = [
    {
        path:'/',
        name:'LoginView',
        component:LoginView
    },
    {
        path:'/indexadmin',
        name:'IndexAdmin',
        component:function() {
            return import('../views/IndexAdmin.vue')
        },
        children:[
            {
            path:"userview",
            name:"UserView",
            component:function() {
                return import('../views/UserView.vue')
                }
            },
        ]
    },
]
// 创建路由对象,并传递规则
const router = new VueRouter({
    routes,
})
// 路由守卫
router.beforeEach((to,from,next) => {
    if (to.name == "LoginView"){
        next()
    }else{
        const username = store.state.username
        if (username==""){
            Message.error("用户身份过期,请重新登陆")
            next({name:"LoginView"})
        }else{
            next()
        }

    }
})
// 导出路由
export default router

